package com.qf.service.impl;

import com.alibaba.excel.EasyExcel;
import com.qf.listener.DoctorListener;
import com.qf.model.entity.Doctor;
import com.qf.dao.DoctorDao;
import com.qf.service.DoctorService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * (Doctor)表服务实现类
 *
 * @author ZhiHuiJun
 * @since 2024-05-02 16:06:57
 */
@Service("doctorService")
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    private DoctorDao doctorDao;

    /**
     * 通过ID查询单条数据
     *
     * @param doctorId 主键
     * @return 实例对象
     */
    @Override
    public Doctor queryById(Long doctorId) {
        return this.doctorDao.queryById(doctorId);
    }

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @Override
    public List<Doctor> queryByPage(Long pageNum) {
        return this.doctorDao.queryAllByLimit();
    }


    @Override
    public List<Doctor> selectDoctorByCondition(Map<String, Object> map) {
        return doctorDao.selectDoctorByCondition(map);
    }

    @Override
    public List<Doctor> selectAllDoctor() {
        return doctorDao.selectAllDoctor();
    }

    /**
     * 新增数据
     *
     * @param doctor 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(Doctor doctor) {
        int i = doctorDao.insert(doctor);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 批量添加
     * @param list
     * @return
     */
    @Override
    public int insertBatch(List<Doctor> list) {
        return doctorDao.insertBatch(list);
    }

    /**
     * 修改数据
     *
     * @param doctor 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(Doctor doctor) {
        int i = doctorDao.update(doctor);
        if (i > 0) {
            return true;
        }
        return false;
    }

    /**
     * 通过主键删除数据
     *
     * @param doctorId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long doctorId) {
        int i = doctorDao.deleteById(doctorId);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteBatch(List<Integer> list) {
        int i = doctorDao.deleteBatch(list);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void doctorExport(HttpServletResponse response) {

        List<Doctor> list = doctorDao.selectAllDoctor();
        try {
            EasyExcel.write(response.getOutputStream(), Doctor.class)
                    .sheet("医生信息").doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean importDoctor(MultipartFile file) {

        try {
            EasyExcel.read(file.getInputStream(), Doctor.class, new DoctorListener(this))
                    .sheet("医生信息").doReadSync();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

}
